// 敏感词
const express = require("express")
const router = express.Router()
const sensitiveDB = require("../../../db/sensitive")
const { sensitiveVer } = require('../../../utils/sensitiveVerification')

// 校验敏感词
router.post('/text', async (req, res) => {
  try {
    const { text } = req.body

    await sensitiveVer(text)

    res.send({
      code: 0,
      mes: '校验通过'
    })
  } catch (error) {
    res.send({
      code: 5,
      mes: error || "服务器异常，请稍后再试"
    })
  }
})

// 获取所有的敏感词
router.get('/all', async (req, res) => {
  try {
    const lists = await sensitiveDB.find({}, {}, {
      sort: {
        create_time: -1
      }
    })

    res.send({
      code: 0,
      mes: "敏感词列表",
      data: lists
    })
  } catch (error) {
    res.send({
      code: 5,
      mes: "服务器异常，请稍后再试"
    })
  }
})


// 更新操作
router.use("/update", require("./update"))

// 发表操作
// 鉴权有没有发表权限
router.use((req, res, next) => {
  if (!req.session.userInfo.allowPublish) {
    return res.send({
      code: 14,
      mes: "您没有发表权限，请联系超级管理员进行添加"
    })
  }
  next()
})


// 添加敏感词
router.post('/add', async (req, res) => {
  try {
    const { name } = req.body

    await sensitiveDB.create({
      name
    })

    res.send({
      code: 0,
      mes: "添加成功"
    })
  } catch (error) {
    res.send({
      code: 5,
      mes: "服务器异常，请稍后再试"
    })
  }
})

module.exports = router